package View.Component
{
	import com.DataAndConstant.GameData;
	import com.DecorationItems;
	import com.WardrobeItemsFinder;
	import com.musicInstrument;

	/**
	 *This class is written in purpose for creating stage in game.
	 * This class will be singleton as we are using same instance all the time. 
	 * @author Yash
	 * 
	 */	
	public class MainStage extends newStage
	{
		// Singleton Making
		private static var getInstance:MainStage;
		
		public static function gameInstance():MainStage
		{
			if(getInstance == null)
				getInstance = new MainStage(new lock());
			return getInstance;
		}
		
		
		//Main Class functionality
		public function MainStage(forcer:lock)
		{
			super();
		}
		public function init():void
		{
			//here setting All the avatar and other assets to visible off so there is nothing presented at start.
			if(!GameData.getInstance().isAvatarSelected)
			{
				this.floorLight.visible = false;
				this.light.visible = false;
				this.DiscoBall.visible = false;
				this.boyAvatar.visible = false;
				this.girlAvatar.visible = false;
				setAllPosterVisible(false);
			}
			addListener();
		}
		public function DiscoBallVisible(flag:Boolean):void
		{
			this.DiscoBall.visible = flag;
		}
		
		public function lightVisible(flag:Boolean):void
		{
			this.floorLight.visible = flag;
				this.light.visible = flag;
		}
		public function setAllPosterVisible(flag:Boolean,chkInex:Array=null):void
		{
			//playing loop to nine times as their is only 9 poster on stage :)
			for(var i:uint = 1; i <= 9; i++)
			{
				if(chkInex != null)
				{
					for(var j:int = 0; j<chkInex.length ; j++)
					{
						if(i != chkInex[j])
						{
							this["poster"+i].visible = flag;
						}
						else
						{
							this["poster"+i].visible = !flag;
							break;
						}
					}
						
				}
			}
		}
		
		private function addListener():void
		{
			if(!GameData.getInstance().isListenerAdded)
			{
				//listener For avatar Selection
				GameData.getInstance().avatarSelection.add(showSelectedAvatar);
				//listener For avatar wardrobe item selection
				GameData.getInstance().avatarItemWardrobe.add(addClipToStage);
				//listener For avatar decoration item
				GameData.getInstance().decorationItemSelection.add(AddDecorationItem);
				///listener For Equpment item/
				GameData.getInstance().equipMentItemSelection.add(AddEquipmentItem);
				//Listener for Music Instrument
				GameData.getInstance().musicInstrumentSelection.add(AddMusicInstrument);
				GameData.getInstance().isListenerAdded = true;
			}
		}
		private function showSelectedAvatar(msg:String):void
		{
			if(GameData.getInstance().isGirlSelected)
			{
				if(!this.girlAvatar.visible)
				{
					this.girlAvatar.visible = true;
					this.boyAvatar.visible = false;
				}
				this.girlAvatar.gotoAndStop(GameData.getInstance().selectedIndex+"Frame");
			}
			else
			{
				if(!this.boyAvatar.visible)
				{
					this.boyAvatar.visible = true;
					this.girlAvatar.visible = false;
				}
				this.boyAvatar.gotoAndStop(GameData.getInstance().selectedIndex+"Frame");
			}
		}
		
		private function addClipToStage(msg:String,data:Object):void
		{
			if(GameData.getInstance().isGirlSelected)
			{
				switch(msg)
				{
					case "Jacket":
					{
						if(this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].jacket_mc.numChildren>0)
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].jacket_mc.removeChildAt(0);
						if(data.name == "Blank")
						{
							
						}
						else
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].jacket_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "Lower":
					{
						if(this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].trouser_mc.numChildren>0)
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].trouser_mc.removeChildAt(0);
						this.girlAvatar.avatar1.trouser_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "T-shirt":
					{
						if(this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].chest_mc.origTop_mc.numChildren>0)
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].chest_mc.origTop_mc.removeChildAt(0);
						this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].chest_mc.origTop_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "Shoes":
					{
						if(this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].shoes_mc.numChildren>0)
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].shoes_mc.removeChildAt(0);
						if(data.name == "Blank")
						{
							
						}
						else
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].shoes_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "Belt":
					{
						if(this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].belt_mc.numChildren>0)
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].belt_mc.removeChildAt(0);
						if(data.name == "Blank")
						{
							
						}
						else
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].belt_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "Locket":
					{
						if(this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].neck_mc.numChildren>0)
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].neck_mc.removeChildAt(0);
						if(data.name == "Blank")
						{
							
						}
						else
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].neck_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "WristBand":
					{
						if(this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].band_mc.numChildren>0)
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].band_mc.removeChildAt(0);
						if(data.name == "Blank")
						{
							
						}
						else
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].band_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "Hair":
					{
						if(this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].hair_mc.numChildren>0)
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].hair_mc.removeChildAt(0);
						this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].hair_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}	
					case "Caps":
					{
						if(this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].cap_mc.numChildren>0)
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].cap_mc.removeChildAt(0);
						if(data.name == "Blank")
						{
							
						}
						else
							this.girlAvatar['avatar'+GameData.getInstance().selectedIndex].cap_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}	
					default:
					{
						break;
					}
				}
			}
			else
			{
				switch(GameData.getInstance().selectedItem)
				{
					case "Jacket":
					{
						if(this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].jacket_mc.numChildren>0)
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].jacket_mc.removeChildAt(0);
						if(data.name == "Blank")
						{
							
						}
						else
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].jacket_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "Lower":
					{
						if(this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].guy_trouser_mc.numChildren>0)
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].guy_trouser_mc.removeChildAt(0);
						this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].guy_trouser_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "T-shirt":
					{
						if(this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].guy_chest_mc.guy_origChest_mc.numChildren>0)
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].guy_chest_mc.guy_origChest_mc.removeChildAt(0);
						this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].guy_chest_mc.guy_origChest_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "Shoes":
					{
						if(this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].shoes_mc.numChildren>0)
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].shoes_mc.removeChildAt(0);
						if(data.name == "Blank")
						{
							
						}
						else
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].shoes_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "Belt":
					{
						if(this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].belt_mc.numChildren>0)
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].belt_mc.removeChildAt(0);
						if(data.name == "Blank")
						{
							
						}
						else
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].belt_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "Locket":
					{
						if(this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].neck_mc.numChildren>0)
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].neck_mc.removeChildAt(0);
						if(data.name == "Blank")
						{
							
						}
						else
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].neck_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "WristBand":
					{
						if(this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].guy_band_mc.numChildren>0)
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].guy_band_mc.removeChildAt(0);
						if(data.name == "Blank")
						{
							
						}
						else
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].guy_band_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}
					case "Hair":
					{
						if(this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].hair_mc.numChildren>0)
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].hair_mc.removeChildAt(0);
						this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].hair_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}	
					case "Caps":
					{
						if(this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].cap_mc.numChildren>0)
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].cap_mc.removeChildAt(0);
						if(data.name == "Blank")
						{
							
						}
						else
							this.boyAvatar['avatar'+GameData.getInstance().selectedIndex].cap_mc.addChildAt(WardrobeItemsFinder.getInstance().wardrobeListItemFinder(data.name),0);
						return;
						break;
					}	
					default:
					{
						break;
					}
				}
			}
		}
		
		private function AddDecorationItem(msg:String,data:Object):void
		{
			this.poster6.visible = true;
			this.poster7.visible = true;
			this.poster8.visible = true;
			this.poster9.visible = true;
			
			var chkStr:String = data.name;
			chkStr = chkStr.slice(0,chkStr.length-1);
			switch(chkStr)
			{
				case "poster":
				{
					if(this.poster6.numChildren > 1)
					{
						this.poster6.removeChildAt(1);
					}
					
					this.poster6.addChildAt((DecorationItems.getInstance().decorationItemFinder(data.name)),1);
					break;
				}
				case "wallpaper":
				{
					if(this.poster7.numChildren > 1)
					{
						this.poster7.removeChildAt(1);
					}
					
					this.poster7.addChildAt(DecorationItems.getInstance().decorationItemFinder(data.name),1);
					break;
				}
				case "curtain":
				{ 
					if(this.poster8.numChildren > 1)
					{
						this.poster8.removeChildAt(1);
					}
					
					this.poster8.addChildAt(DecorationItems.getInstance().decorationItemFinder(data.name),1);
					break;
				}
				case "color":
				{
					if(this.poster9.numChildren > 1)
					{
						this.poster9.removeChildAt(1);
					}
					
					this.poster9.addChildAt(DecorationItems.getInstance().decorationItemFinder(data.name),1);
					break;
				}
				default:
				{
					break;
				}
			}
		}
		private function AddEquipmentItem(msg:String,data:Object):void
		{
			this.poster1.visible = true;
			this.poster2.visible = true;
			this.poster3.visible = true;
			var obj:Object = data;
			switch(obj.name)
			{
				case "MirrorBall":
				{
					if(this.poster1.numChildren > 1)
					{
						this.poster1.removeChildAt(1);
					}
					this.poster1.addChildAt(DecorationItems.getInstance().equipmentMenuItem(obj.name),1);
					break;
				}
				case "Smoke":
				{
					if(this.poster2.numChildren > 1)
					{
						this.poster2.removeChildAt(1);
					}
					this.poster2.addChildAt(DecorationItems.getInstance().equipmentMenuItem(obj.name),1);
					break;
				}
				case "Light":
				{
					if(this.poster3.numChildren > 1)
					{
						this.poster3.removeChildAt(1);
					}
					this.poster3.addChildAt(DecorationItems.getInstance().equipmentMenuItem(obj.name),1);
					break;
				}
				default:
				{
					break;
				}
			} 
		}
		private function AddMusicInstrument(msg:String,data:Object):void
		{
			this.poster5.visible = true;
			var chkStr:String = data.name;
			chkStr = chkStr.slice(0,chkStr.length-1);
			switch(chkStr)
			{
				case "Guitar":
				{
					if(this.poster5.numChildren > 1)
					{
						this.poster5.removeChildAt(1);
					}
					
					this.poster5.addChildAt(musicInstrument.getInstance().decorationItemFinder(data.name),1); 
					break;
				}
				case "Drum":
				{
					if(this.poster5.numChildren > 1)
					{
						this.poster5.removeChildAt(1);
					}
					
					this.poster5.addChildAt(musicInstrument.getInstance().decorationItemFinder(data.name),1); 
					break;
				}
				case "check":
				{ 
					if(this.poster5.numChildren > 1)
					{
						this.poster5.removeChildAt(1);
					}
					
					this.poster5.addChildAt(musicInstrument.getInstance().decorationItemFinder(data.name),1); 
					break;
				}
					
				default:
				{
					break;
				}
			}
		}
	}
}
class lock{}